package com.ljl.pet.service.impl;

import com.ljl.basic.exception.BusinessException;
import com.ljl.basic.service.IBaiduAiAuditService;
import com.ljl.basic.service.impl.BaseServiceImpl;
import com.ljl.basic.util.LoginContext;
import com.ljl.org.domain.Employee;
import com.ljl.org.mapper.EmployeeMapper;
import com.ljl.pet.domain.Pet;
import com.ljl.pet.domain.PetDetail;
import com.ljl.pet.domain.PetOnlineAuditLog;
import com.ljl.pet.mapper.PetDetailMapper;
import com.ljl.pet.mapper.PetMapper;
import com.ljl.pet.mapper.PetOnlineAuditLogMapper;
import com.ljl.pet.service.IPetService;
import com.ljl.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Transactional
    @Override
    public void save(Pet pet) {
        super.save(pet);

        PetDetail petDetail = pet.getPetDetail();
        if (petDetail != null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.save(petDetail);
        }


    }
    @Transactional
    @Override
    public void del(Long id) {
        super.del(id);
        petDetailMapper.delPetDetailByPetId(id);


    }

    @Transactional
    @Override
    public void update(Pet pet) {
        super.update(pet);
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail != null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.updatePetDetailByPetId(petDetail);
        }
    }
    @Transactional
    @Override
    public void onsale(List<Long> ids, HttpServletRequest request) {
        Logininfo loginUser = LoginContext.getLoginUser(request);
        Employee employee = employeeMapper.findByLogininfoId(loginUser.getId());
        for (Long id : ids) {
            Pet pet = petMapper.findById(id);
            Boolean textBoo = baiduAiAuditService.textAudit(pet.getName());
            Boolean imageBoo = baiduAiAuditService.imageAudit(pet.getResources());
            PetOnlineAuditLog log = new PetOnlineAuditLog();
            if (textBoo && imageBoo){
                pet.setState(1);
                pet.setOnsaletime(new Date());
                petMapper.update(pet);

                log.setState(1);
                log.setPetId(pet.getId());
                log.setAuditId(employee.getId());
                log.setAuditTime(new Date());
                log.setNote("审核成功");
                petOnlineAuditLogMapper.save(log);
            } else {
                pet.setState(0);
                pet.setOffsaletime(new Date());
                petMapper.update(pet);
                log.setState(1);
                log.setPetId(pet.getId());
                log.setAuditId(employee.getId());
                log.setAuditTime(new Date());
                log.setNote("审核失败");
                petOnlineAuditLogMapper.save(log);
                throw new BusinessException("图片或名字不合规！");
            }

        }

    }
    @Transactional
    @Override
    public void offsale(List<Long> ids) {
        Date offsaletime = new Date();
        petMapper.offsale(offsaletime,ids);

    }
}
